<?php


namespace app\adminbackstage\controller;


use app\adminbackstage\model\Advertisement;
use think\facade\Request;
use think\facade\Config;

/***  广告管理
* Class AdvertisementController
 * @package app\adminfocus\controller
 */
class AdvertisementController
{
    //广告列表
    public function advertisementList(){
        $page = Request::param('page');
        $num = Request::param('num');
        $type = Request::param('type');//广告位置
        $explain = Request::param('explain');//广告名称
        $starttime = Request::param('starttime');//广告展示开始时间
        $endtime = Request::param('endtime');//广告展示结束时间
        $enable = Request::param('enable');
        $position = Request::param('position'); //广告位置 由上至下 1为头图下方第一个以此类推
        $f_page = Request::param('f_page'); //作用页面 页面名

        $where = [];
//        if(!empty($enable)){
//            if($enable == -1){
//                $enable = 0;
//            }
//            $where[] = ['enable','=',$enable];
//        }
        if(!empty($enable)){
            if($enable == -1){
                $enable = 0;
            }
            $where[] = ['enable','=',$enable];
        }
        if(!empty($type)){
            $where[] = ['type','=',$type];
        }
        if(!empty($position)){
            $where[] = ['position','=',$position];
        }
        if(!empty($f_page)){
            $where[] = ['f_page','=',$f_page];
        }
        if(!empty($explain)){
            $where[] = ['explain','like',"%$explain%"];
        }
        if(!empty($starttime)){
            $starttime = strtotime("starttime");
            $where[] = ['start_time','>=',$starttime];
        }
        if(!empty($endtime)){
            $endtime = strtotime("endtime");
            $where[] = ['end_time','<=',$endtime];
        }
        $advertisementList = Advertisement::advertisementList($where,$page,$num);
        $listinfo = [];
        foreach ($advertisementList as $item){
            if($item['sort'] == 101){ //排序101表示清空  排序值不在前端显示
                $item['sort'] = '';
            }
            if(!empty($item['start_time'])){
                $item['start_time'] = date('Y-m-d H:i',$item['start_time']);
            }
            if(!empty($item['end_time'])){
                $item['end_time'] = date('Y-m-d H:i',$item['end_time']);
            }
            
            if($item['enable']==1){
                $item['enable_str']= '已启用';
            }
            else{
                $item['enable_str']= '已禁用';
            }
            
            if($item['type']==1){
                $item['type_str'] = 'PC端';
            }
            elseif($item['type']==2){
                $item['type_str'] = '手机端';
            }
            elseif($item['type']==3){
                $item['type_str'] = '商家个人中心';
            }
            elseif($item['type']==4){
                $item['type_str'] = '推广';
            }
            
            if(!empty($item['addtime'])){
                $item['addtime'] = date('Y-m-d H:i',$item['addtime']);
            }
            if(!empty($item['img'])){
                if(strpos($item['img'],"img.alicdn.com") !== false){
                    $item['img_url'] =  $item['img'];
                    $item['img'] =  $item['img'];
                }
                elseif(strpos($item['img'],"http") !== false){
                    $item['img_url'] =  $item['img'];
                    $item['img'] =  $item['img'];
                }
                else{
                    $item['img_url'] =  Config::get('app.pomegranate_url').$item['img'];
                    $item['img'] =  Config::get('app.pomegranate_url').$item['img'];
                }
//                $item['img'] =  Config::get('app.pomegranate_url').$item['img'];
            }
            
            $listinfo[] = $item;
        }

        //分页
        $count = Advertisement::advertisementCount($where);

        return json(['code' => 1, 'msg' => lang(''), 'data' => $listinfo, 'count' => $count]);
    }
    
    public function advertisementInfo(){
        $id =  Request::param('id');
        $where = [];
        $where[] = ['id','=',$id];
        $advertisementView = Advertisement::advertisementView($where);
        if($advertisementView['sort'] == 101){ //排序101表示清空  排序值不在前端显示
            $advertisementView['sort'] = '';
        }
        if(!empty($advertisementView['start_time'])){
            $advertisementView['start_time'] = date('Y-m-d H:i',$advertisementView['start_time']);
        }
        if(!empty($advertisementView['end_time'])){
            $advertisementView['end_time'] = date('Y-m-d H:i',$advertisementView['end_time']);
        }
        
        if($advertisementView['type']==1){
            $advertisementView['type_str'] = 'PC端';
        }
        elseif($advertisementView['type']==2){
            $advertisementView['type_str'] = '手机端';
        }
        elseif($advertisementView['type']==3){
            $advertisementView['type_str'] = '商家个人中心';
        }
        elseif($advertisementView['type']==4){
            $advertisementView['type_str'] = '推广';
        }
        
        if($advertisementView['enable']==1){
            $advertisementView['enable_str']= '已启用';
        }
        else{
            $advertisementView['enable_str']= '已禁用';
        }
        
        if(!empty($advertisementView['addtime'])){
            $advertisementView['addtime'] = date('Y-m-d H:i',$advertisementView['addtime']);
        }
        if(!empty($advertisementView['img_str'])){
            if(strpos($advertisementView['img_str'],"img.alicdn.com") !== false){
                $advertisementView['img_str_url'] =  $advertisementView['img_str'];
            }
            elseif(strpos($advertisementView['img_str'],"http") !== false){
                $advertisementView['img_str_url'] =  $advertisementView['img_str'];
            }
            else{
                $advertisementView['img_str_url'] =  Config::get('app.pomegranate_url').$advertisementView['img'];
            }
//            $advertisementView['img_str'] =  Config::get('app.pomegranate_url').$advertisementView['img'];
        
        }
        return json(['code' => 1, 'msg' => lang(''), 'data' => $advertisementView, 'count' => '']);
    }

    //banner 添加
    public function advertisementAdd(){
        $type = Request::param('type');//类型  1PC端  2手机端  3商家个人中心
        $explain = Request::param('explain'); //banner名称  / 说明
        $img = Request::param('img'); //图片地址
        $link = Request::param('link'); //跳转地址
        $sort = Request::param('sort'); //排序
        $starttime = Request::param('starttime'); //展示开始时间
        $endtime = Request::param('endtime'); //展示结束时间
        $position = Request::param('position'); //广告位置 由上至下 1为头图下方第一个以此类推
        $f_page = Request::param('f_page'); //作用页面 页面名

        $data = [
            'type' => $type,
            'explain' => $explain,
            'img' => $img,
            'link' => $link,
            'sort' => $sort,
            'position' => $position,
            'f_page' => $f_page,
            'start_time' => strtotime($starttime),
            'end_time' => strtotime($endtime),
            'addtime' => time(),
        ];
        Advertisement::advertisementAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

    //banner 修改
    public function advertisementEdit(){
        $id = Request::param('id');
        $type = Request::param('type');//类型  1PC端  2手机端  3商家个人中心
        $explain = Request::param('explain'); //banner名称  / 说明
        $img = Request::param('img'); //图片地址
        $link = Request::param('link'); //跳转地址
        $sort = Request::param('sort'); //排序
        $starttime = Request::param('starttime'); //展示开始时间
        $endtime = Request::param('endtime'); //展示结束时间
        $position = Request::param('position'); //广告位置 由上至下 1为头图下方第一个以此类推
        $f_page = Request::param('f_page'); //作用页面 页面名
        
        $data = [];
        if(!empty($type)){
            $data['type'] = $type;
        }
        if(!empty($explain)){
            $data['explain'] = $explain;
        }
        if(!empty($img)){
            $data['img'] = $img;
        }
        if(!empty($link)){
            $data['link'] = $link;
        }
        if(!empty($sort)){
            $data['sort'] = $sort;
        }
        
        if(!empty($position)){
            $data['position'] = $position;
        }
        if(!empty($f_page)){
            $data['f_page'] = $f_page;
        }
        
        if(!empty($starttime)){
            $data['start_time'] = strtotime($starttime);
        }
        if(!empty($endtime)){
            $data['end_time'] = strtotime($endtime);
        }
        Advertisement::advertisementEdit(['id'=>$id],$data);
        
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

    //广告上线，下线
    public function advertisementDel(){
        $id = Request::param('id');
        $enable = Request::param('enable');
        if($enable == 1){
            Advertisement::advertisementEdit(['id'=>$id],['enable'=>1]);
        }
        else{
            Advertisement::advertisementEdit(['id'=>$id],['enable'=>0]);
        }
        
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
}